package com.yss.reportworld.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yss.common.core.constant.RequestDataHelper;
import com.yss.common.core.enums.YssStatusEnum;
import com.yss.common.core.exception.BusinessException;
import com.yss.common.core.utils.JdbcUtil;
import com.yss.common.core.utils.StringUtils;
import com.yss.common.core.utils.easyexecl.*;
import com.yss.common.core.utils.sql.SqlUtil;
import com.yss.common.core.utils.uuid.IdUtils;
import com.yss.reportworld.domain.kettle.*;
import com.yss.reportworld.mapper.kettle.*;
import com.yss.reportworld.service.IDataCustomerService;
import com.yss.reportworld.service.IKettleService;
import com.yss.reportworld.util.TableInfoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class KettleServiceImpl implements IKettleService {
    @Autowired
    private EtlJobTaskMapper etlJobTaskMapper;
    @Autowired
    private EtlConfigDynamicFieldMapper etlConfigDynamicFieldMapper;
    @Autowired
    private EtlJobMapper etlJobMapper;
    @Autowired
    private EtlJobTaskDetailMapper etlJobTaskDetailMapper;
    @Autowired
    private EtlResourceMapper etlResourceMapper;
    @Autowired
    private EtlJobDetailMapper etlJobDetailMapper;
    @Autowired
    private EtlResourceTypeMapper etlResourceTypeMapper;
    @Qualifier("dataSource")
    @Autowired
    private DataSource dataSource;
    @Autowired
    private IDataCustomerService dataCustomerService;

    @Override
    public void downKettleExecl(HttpServletResponse response, EtlJobTask etlJobTask) {
        ExcelWriter excelWriter = null;
        try {
            TableInfoUtil.handleTableVersion(etlJobTask.getHisVersion());
            // 采集任务
            QueryWrapper<EtlJobTask> etlJobTaskQueryWrapper = new QueryWrapper<>();
            etlJobTaskQueryWrapper.in("T1.ID", etlJobTask.getIds());
            etlJobTaskQueryWrapper.orderByAsc("T1.ORDER_NUM");
            etlJobTaskQueryWrapper.eq("T1.RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
            List<EtlJobTask> listJobTabk = etlJobTaskMapper.queryEtlJobTask(etlJobTaskQueryWrapper);
            listJobTabk = dataCustomerService.handleBusinessPrimaryCustomer(listJobTabk, etlJobTask.getCustomerId(), EtlJobTask.class);
            if (StringUtils.isEmpty(listJobTabk)) {
                throw new BusinessException("没有要导出的任务信息，请检查数据！");
            }
            List<String> listTaskId = listJobTabk.stream()
                    .map(e -> new String(e.getId()))
                    .collect(Collectors.toList());
            // 任务作业关联
            QueryWrapper<EtlJobTaskDetail> taskDetailQueryWrapper = new QueryWrapper<>();
            taskDetailQueryWrapper.in("T1.TASK_ID", listTaskId);
            taskDetailQueryWrapper.orderByAsc("T1.ORDER_NUM");
            taskDetailQueryWrapper.eq("T1.RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
            List<EtlJobTaskDetail> listTaskDetail = etlJobTaskDetailMapper.queryEtlJobTaskDetail(taskDetailQueryWrapper);
            listTaskDetail = dataCustomerService.handleBusinessPrimaryCustomer(listTaskDetail, etlJobTask.getCustomerId(), EtlJobTaskDetail.class);
            if (StringUtils.isEmpty(listTaskDetail)) {
                throw new BusinessException("任务下面没有获取到任何作业信息，请检查数据！");
            }
            //获取所有的采集作业的Id
            List<String> listJobId = listTaskDetail.stream()
                    .map(e -> new String(e.getJobId()))
                    .collect(Collectors.toList());
            // 采集作业
            QueryWrapper<EtlJob> etlJobQueryWrapper = new QueryWrapper<>();
            etlJobQueryWrapper.in("T1.ID", listJobId);
            etlJobQueryWrapper.orderByAsc("T1.LEVEL_ID");
            etlJobQueryWrapper.eq("T1.RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
            List<EtlJob> listJob = etlJobMapper.queryEtlJob(etlJobQueryWrapper);
            listJob = dataCustomerService.handleBusinessPrimaryCustomer(listJob, etlJobTask.getCustomerId(), EtlJob.class);
            if (StringUtils.isEmpty(listJob)) {
                throw new BusinessException("没有获取到任何采集作业信息，请检查数据！");
            }
            // 处理目标资源和来源资源的 数据源和资源拼接
            Set<String> resourceId = new HashSet<>();
            this.handleJobResource(listJob, resourceId);
            if (StringUtils.isEmpty(resourceId)) {
                throw new BusinessException("没有获取到资源信息，请检查数据！");
            }
            // 采集作业明细
            QueryWrapper<EtlJobDetail> etlJobDetailQueryWrapper = new QueryWrapper<>();
            etlJobDetailQueryWrapper.in("T1.JOB_ID", listJobId);
            etlJobDetailQueryWrapper.eq("T1.RW_DB_DATABASE_TYPE", etlJobTask.getDataBaseType());
            etlJobDetailQueryWrapper.orderByAsc("T1.JOB_ID,T1.ORDER_NUM");
            etlJobDetailQueryWrapper.eq("T1.RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
            List<EtlJobDetail> listJobDetail = etlJobDetailMapper.queryEtlJobDetail(etlJobDetailQueryWrapper);
            listJobDetail = dataCustomerService.handleBusinessPrimaryCustomer(listJobDetail, etlJobTask.getCustomerId(), EtlJobDetail.class);
            if (StringUtils.isEmpty(listJobDetail)) {
                throw new BusinessException("没有获取到采集作业明细信息，请检查数据！");
            }
            //获取所有的采集作业明细的目表资源英文名，中间表的资源没有对应的来源和目标资源
            Set<String> resourceEname = listJobDetail.stream()
                    .map(e -> new String(e.getTargetResEname()))
                    .collect(Collectors.toSet());
            // 采集资源EtlJobDetailMapper
            QueryWrapper<EtlResource> etlResourceQueryWrapper = new QueryWrapper<>();
            etlResourceQueryWrapper.and(wrapper -> wrapper.in("T1.ID", resourceId).or().in("T1.ENAME", resourceEname));
            etlResourceQueryWrapper.orderByAsc("T1.LEVEL_ID,T1.TYPE_ID");
            etlResourceQueryWrapper.eq("T1.RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
            List<EtlResource> listResource = etlResourceMapper.queryEtlResource(etlResourceQueryWrapper);
            listResource = dataCustomerService.handleBusinessPrimaryCustomer(listResource, etlJobTask.getCustomerId(), EtlResource.class);
            //参数信息，每次都是全量的导出
            QueryWrapper<EtlConfigDynamicField> etlConfigDynamicFieldQueryWrapper = new QueryWrapper<>();
            etlConfigDynamicFieldQueryWrapper.orderByAsc("T1.SORT");
            etlConfigDynamicFieldQueryWrapper.eq("T1.RW_DB_STATUS", YssStatusEnum.NORMAL.getCode());
            List<EtlConfigDynamicField> listConfigDynamicField = etlConfigDynamicFieldMapper.queryEtlConfigDynamicField(etlConfigDynamicFieldQueryWrapper);
            listConfigDynamicField = dataCustomerService.handleBusinessPrimaryCustomer(listConfigDynamicField, etlJobTask.getCustomerId(), EtlConfigDynamicField.class);
            // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
            HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                    new HorizontalCellStyleStrategy(EasyExeclUtil.headWriteCellStyle(), EasyExeclUtil.contentWriteCellStyle());
            excelWriter = EasyExcel.write(response.getOutputStream())
                    .inMemory(true)
                    .registerWriteHandler(new WaterMarkHandler())
                    .excelType(ExcelTypeEnum.XLSX)
                    .registerWriteHandler(horizontalCellStyleStrategy)
                    .registerWriteHandler(new Custemhandler())
                    .build();
            WriteSheet sheetResource = EasyExcel.writerSheet(0, EtlResource.SHEET_NAME).build();
            WriteSheet sheetField = EasyExcel.writerSheet(1, EtlConfigDynamicField.SHEET_NAME).build();
            WriteSheet sheetTask = EasyExcel.writerSheet(2, EtlJobTask.SHEET_NAME).build();
            WriteSheet sheetJob = EasyExcel.writerSheet(3, EtlJob.SHEET_NAME).build();
            WriteSheet sheetJobDetail = EasyExcel.writerSheet(4, EtlJobDetail.SHEET_NAME).build();
            WriteSheet sheetTaskDetail = EasyExcel.writerSheet(5, EtlJobTaskDetail.SHEET_NAME).build();

            WriteTable tableResource = EasyExcel.writerTable().head(EtlResource.class).needHead(true).build();
            WriteTable tableField = EasyExcel.writerTable().head(EtlConfigDynamicField.class).needHead(true).build();
            WriteTable tableTask = EasyExcel.writerTable().head(EtlJobTask.class).needHead(true).build();
            WriteTable tableJob = EasyExcel.writerTable().head(EtlJob.class).needHead(true).build();
            WriteTable tableJobDetail = EasyExcel.writerTable().head(EtlJobDetail.class).needHead(true).build();
            WriteTable tableTaskDetail = EasyExcel.writerTable().head(EtlJobTaskDetail.class).needHead(true).build();

            EasyExeclUtil.write(listResource, excelWriter, sheetResource, tableResource);
            EasyExeclUtil.write(listConfigDynamicField, excelWriter, sheetField, tableField);
            EasyExeclUtil.write(listJobTabk, excelWriter, sheetTask, tableTask);
            EasyExeclUtil.write(listJob, excelWriter, sheetJob, tableJob);
            EasyExeclUtil.write(listJobDetail, excelWriter, sheetJobDetail, tableJobDetail);
            EasyExeclUtil.write(listTaskDetail, excelWriter, sheetTaskDetail, tableTaskDetail);
        } catch (Exception ex) {
            log.error("报表世界ETL采集文件导出失败" + ex.getMessage());
            throw new BusinessException("报表世界ETL采集文件导出失败！" + ex.getMessage());
        } finally {
            if (null != excelWriter) {
                excelWriter.finish();
            }
            RequestDataHelper.removeThreadLocal();
        }
    }

    //处理采集任务的来源资源和目标资源信息
    private void handleJobResource(List<EtlJob> listJob, Set<String> resourceId) {
        for (EtlJob job : listJob) {
            if (StringUtils.isNull(job) || StringUtils.isEmpty(job.getId())) {
                continue;
            }
            job.setListTargetResource(etlResourceMapper.queryTargetEtlResourceByJobId(job.getId()));
            job.setListSourceResource(etlResourceMapper.querySouRceEtlResourceByJobId(job.getId()));
            if (StringUtils.isEmpty(job.getListTargetResource())) {
                throw new BusinessException("任务【" + job.getJobName() + "】的目标资源为空，请检查数据！");
            }
            if (StringUtils.isEmpty(job.getListSourceResource())) {
                throw new BusinessException("任务【" + job.getJobName() + "】的来源资源为空，请检查数据！");
            }
            StringJoiner targetResource = new StringJoiner(",");
            StringJoiner sourceResource = new StringJoiner(",");
            // 目表资源集合
            for (EtlResource resource : job.getListTargetResource()) {
                if (StringUtils.isNull(resource) || StringUtils.isEmpty(resource.getDsSource())
                        || StringUtils.isEmpty(resource.getEname())) {
                    throw new BusinessException("任务【" + job.getJobName() + "】目表资源对应的数据源或者英文名称为空，请检查数据！");
                }
                resourceId.add(resource.getId());
                targetResource.add(resource.getDsSource() + "." + resource.getEname());
            }
            // 来源资源集合
            for (EtlResource resource : job.getListSourceResource()) {
                if (StringUtils.isNull(resource) || StringUtils.isEmpty(resource.getDsSource())
                        || StringUtils.isEmpty(resource.getEname())) {
                    throw new BusinessException("任务【" + job.getJobName() + "】来源资源对应的数据源或者英文名称为空，请检查数据！");
                }
                resourceId.add(resource.getId());
                sourceResource.add(resource.getDsSource() + "." + resource.getEname());
            }
            job.setTargetResourceId(targetResource.toString());
            job.setSourceResourceId(sourceResource.toString());
        }
    }


    // 资源类型和资源的MAP  key是name value是id
    private Map<String, String> etlResourceTypeMap = new HashMap<>();
    // 资源集合 key 是cname value是id
    private Map<String, String> etlResourceMap = new HashMap<>();
    // 任务信息集合 key 是taskName value是id
    private Map<String, String> etlJobTaskMap = new HashMap<>();
    // 作业信息集合 key 是jobNmae value是id
    private Map<String, String> etlJobMap = new HashMap<>();
    // 参数分组
    private Map<String, String> etlParamerGroupMap = new HashMap<>();
    // 模板名称
    private Map<String, String> etlTemplateMap = new HashMap<>();
    // etl层级信息
    private Map<String, String> etlLevelMap = new HashMap<>();

    // 初始化获取分组，模板和层级信息
    private void initKettleSql(String version) {
        ResultSet rs = null;
        PreparedStatement pStemtSelect = null;
        Connection connection = null;
        try {
            // 初始化
            etlResourceTypeMap = new HashMap<>();
            etlResourceMap = new HashMap<>();
            etlJobTaskMap = new HashMap<>();
            etlJobMap = new HashMap<>();
            etlParamerGroupMap = new HashMap<>();
            etlTemplateMap = new HashMap<>();
            etlLevelMap = new HashMap<>();
            connection = dataSource.getConnection();
            if (null == connection) {
                throw new BusinessException("获取数据库连接出错，请稍后重试！");
            }
            String versionSuffix = TableInfoUtil.getVersionSuffix(version);
            String sql = "SELECT ID,NAME FROM RW_DB_ETL_PARAMER_GROUP" + versionSuffix;
            pStemtSelect = connection.prepareStatement(sql);
            rs = pStemtSelect.executeQuery();
            while (rs.next()) {
                etlParamerGroupMap.put(rs.getString("NAME"), rs.getString("ID"));
            }
            rs.close();
            sql = "SELECT ID,TEMPLATE_NAME FROM RW_DB_ETL_TEMPLATE" + versionSuffix;
            pStemtSelect = connection.prepareStatement(sql);
            rs = pStemtSelect.executeQuery();
            while (rs.next()) {
                etlTemplateMap.put(rs.getString("TEMPLATE_NAME"), rs.getString("ID"));
            }
            rs.close();
            sql = "SELECT ID,LEVEL_NAME FROM RW_DB_ETL_JOB_LEVEL" + versionSuffix;
            pStemtSelect = connection.prepareStatement(sql);
            rs = pStemtSelect.executeQuery();
            while (rs.next()) {
                etlLevelMap.put(rs.getString("LEVEL_NAME"), rs.getString("ID"));
            }
            rs.close();
        } catch (Exception ex) {
            throw new BusinessException("获取ETL的分组，模板和层级信息出错！" + ex.getMessage());
        } finally {
            try {
                JdbcUtil.close(rs, pStemtSelect, connection);
            } catch (Exception e) {
                log.error(e.getMessage());
            }
        }
    }

    /**
     * 按照任务导出，去生成脚本
     *
     * @param multipartFile
     * @return
     */
    public String getKettleSql(MultipartFile multipartFile, String hisVersion) {
        StringBuffer sqlBuffer = new StringBuffer();
        ExcelReader excelReader = null;
        try {
            String multipartFileName = multipartFile.getOriginalFilename();
            // 获取文件后缀
            String prefix = multipartFileName.substring(multipartFileName.lastIndexOf("."));
            if (!StringUtils.oneCase(prefix.toUpperCase(), ".XLSX")) {
                throw new BusinessException("请导入xlsx格式文件");
            }
            excelReader = EasyExcel.read(multipartFile.getInputStream()).build();
            EasyExeclUtil.setReadOrder(excelReader, EtlConfigDynamicField.SHEET_NAME, EtlResource.SHEET_NAME,
                    EtlJobTask.SHEET_NAME, EtlJob.SHEET_NAME, EtlJobDetail.SHEET_NAME, EtlJobTaskDetail.SHEET_NAME);
            ExcelListener<EtlConfigDynamicField> etlConfigDynamicFieldListener = new ExcelListener<>();
            ExcelListener<EtlResource> etlResourceListener = new ExcelListener<>();
            ExcelListener<EtlJobTask> etlJobTaskListener = new ExcelListener<>();
            ExcelListener<EtlJob> etlJobListener = new ExcelListener<>();
            ExcelListener<EtlJobDetail> etlJobDetailListener = new ExcelListener<>();
            ExcelListener<EtlJobTaskDetail> etlJobTaskDetailListener = new ExcelListener<>();
            // 从文件中读取到数据
            ReadSheet etlConfigDynamicFieldSheet = EasyExcel.readSheet(EtlConfigDynamicField.SHEET_NAME).head(EtlConfigDynamicField.class).headRowNumber(1).registerReadListener(etlConfigDynamicFieldListener).build();
            ReadSheet etlResourceSheet = EasyExcel.readSheet(EtlResource.SHEET_NAME).head(EtlResource.class).headRowNumber(1).registerReadListener(etlResourceListener).build();
            ReadSheet etlJobTaskSheet = EasyExcel.readSheet(EtlJobTask.SHEET_NAME).head(EtlJobTask.class).headRowNumber(1).registerReadListener(etlJobTaskListener).build();
            ReadSheet etlJobSheet = EasyExcel.readSheet(EtlJob.SHEET_NAME).head(EtlJob.class).headRowNumber(1).registerReadListener(etlJobListener).build();
            ReadSheet etlJobDetailSheet = EasyExcel.readSheet(EtlJobDetail.SHEET_NAME).head(EtlJobDetail.class).headRowNumber(1).registerReadListener(etlJobDetailListener).build();
            ReadSheet etlJobTaskDetailSheet = EasyExcel.readSheet(EtlJobTaskDetail.SHEET_NAME).head(EtlJobTaskDetail.class).headRowNumber(1).registerReadListener(etlJobTaskDetailListener).build();
            excelReader.read(etlConfigDynamicFieldSheet, etlResourceSheet, etlJobTaskSheet, etlJobSheet, etlJobDetailSheet, etlJobTaskDetailSheet);
            TableInfoUtil.handleTableVersion(hisVersion); // 替换表名
            this.initKettleSql(hisVersion);
            List<EtlResource> etlResourceList = etlResourceListener.getDatas();
            // 生成资源的插入语句
            sqlBuffer.append(this.getEtlResourceSql(etlResourceList));
            // 生成 参数信息 插入语句
            List<EtlConfigDynamicField> etlConfigDynamicFieldList = etlConfigDynamicFieldListener.getDatas();
            sqlBuffer.append(this.getEtlConfigDynamicFieldSql(etlConfigDynamicFieldList));
            // 生成任务信息 插入语句
            List<EtlJobTask> etlJobTaskList = etlJobTaskListener.getDatas();
            sqlBuffer.append(this.getEtlJobTaskSql(etlJobTaskList));
            // 作业列表信息 插入语句
            List<EtlJob> etlJobList = etlJobListener.getDatas();
            sqlBuffer.append(getEtlJobSql(etlJobList));
            List<EtlJobDetail> etlJobDetailList = etlJobDetailListener.getDatas();
            sqlBuffer.append(this.getEtlJobDetailSql(etlJobDetailList));
            List<EtlJobTaskDetail> etlJobTaskDetailList = etlJobTaskDetailListener.getDatas();
            sqlBuffer.append(getEtlJobTaskDetailSql(etlJobTaskDetailList));
        } catch (Exception ex) {
            log.error(ex.getMessage());
            throw new BusinessException("生成ETL的SQL语句失败：" + ex.getMessage());
        } finally {
            RequestDataHelper.removeThreadLocal();
        }
        return sqlBuffer.toString();
    }

    /**
     * 生成EtlResource和EtlResourceType的插入语句
     *
     * @param etlResourceList 文件里的资源列表
     * @return
     */
    private String getEtlResourceSql(List<EtlResource> etlResourceList) {
        // 获取资源类型
        if (StringUtils.isEmpty(etlResourceList)) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        for (EtlResource etlResource : etlResourceList) {
            etlResourceTypeMap.put(etlResource.getResourceName(), null);
            etlResourceMap.put(etlResource.getDsSource() + "." + etlResource.getEname(), null);
        }
        // 查询标准库是否存在
        Map<String, String> etlResourceTypeStaMap = etlResourceTypeMapper.selectList(new QueryWrapper<>()).
                stream().collect(Collectors.toMap(e -> e.getName(), e -> e.getId()));
        etlResourceTypeMap.putAll(etlResourceTypeStaMap);
        for (Map.Entry<String, String> entry : etlResourceTypeMap.entrySet()) {
            if (StringUtils.isNotEmpty(entry.getValue())) {
                continue;
            }
            String resourceTypeId = IdUtils.fastSimpleUUID();
            entry.setValue(resourceTypeId);
            // 生成EtlResourceType插入语句
            buffer.append("INSERT INTO ETL_RESOURCE_TYPE (ID, NAME, CREATE_BY, CREATE_TIME, UPDATE_BY, UPDATE_TIME, PARENT_ID)")
                    .append(" VALUES('" + resourceTypeId + "', '" + entry.getKey() + "', NULL, SYSDATE, NULL, NULL, NULL);")
                    .append("\r\n");
        }
        // 查询标准库是否存在
        Map<String, String> etlResourceMapStaMap = etlResourceMapper.selectList(new QueryWrapper<>()).
                stream().collect(Collectors.toMap(e -> e.getDsSource() + "." + e.getEname(), e -> e.getId()));
        etlResourceMap.putAll(etlResourceMapStaMap);
        for (Map.Entry<String, String> entry : etlResourceMap.entrySet()) {
            if (StringUtils.isNotEmpty(entry.getValue())) {
                continue;
            }
            entry.setValue(IdUtils.fastSimpleUUID());
        }
        // 生成etlResource的插入语句
        for (EtlResource etlResource : etlResourceList) {
            String resourceId = etlResourceMap.get(etlResource.getDsSource() + "." + etlResource.getEname());
            etlResource.setId(resourceId);
            etlResource.setTypeId(etlResourceTypeMap.get(etlResource.getResourceName()));
            etlResource.setLevelId(etlLevelMap.get(etlResource.getLevelName()));
        }
        buffer.append(SqlUtil.getSqlByList("ETL_RESOURCE", etlResourceList, EtlResource.class));
        return buffer.toString();
    }

    /**
     * 生成 参数信息 的插入语句
     *
     * @param etlConfigDynamicFieldList
     * @return
     */
    private String getEtlConfigDynamicFieldSql(List<EtlConfigDynamicField> etlConfigDynamicFieldList) {
        if (StringUtils.isEmpty(etlConfigDynamicFieldList)) {
            return "";
        }
        for (EtlConfigDynamicField etlConfigDynamicField : etlConfigDynamicFieldList) {
            String etlConfigDynamicFieldId = IdUtils.fastSimpleUUID();
            etlConfigDynamicField.setId(etlConfigDynamicFieldId);
            etlConfigDynamicField.setGroupId(etlParamerGroupMap.get(etlConfigDynamicField.getGroupName()));
        }
        return SqlUtil.getSqlByList("ETL_CONFIG_DYNAMIC_FIELD", etlConfigDynamicFieldList, EtlConfigDynamicField.class);
    }

    /**
     * 生成 任务信息 的插入语句
     *
     * @param etlJobTaskList
     * @return
     */
    private String getEtlJobTaskSql(List<EtlJobTask> etlJobTaskList) {
        if (StringUtils.isEmpty(etlJobTaskList)) {
            return "";
        }
        for (EtlJobTask etlJobTask : etlJobTaskList) {
            etlJobTaskMap.put(etlJobTask.getTaskName(), null);
        }
        // 查询标准库是否存在
        Map<String, String> etlJobTaskMapStaMap = etlJobTaskMapper.selectList(new QueryWrapper<>()).
                stream().collect(Collectors.toMap(e -> e.getTaskName(), e -> e.getId()));
        etlJobTaskMap.putAll(etlJobTaskMapStaMap);
        for (Map.Entry<String, String> entry : etlJobTaskMap.entrySet()) {
            if (StringUtils.isNotEmpty(entry.getValue())) {
                continue;
            }
            entry.setValue(IdUtils.fastSimpleUUID());
        }
        // 生成EtlJobTask的插入语句
        for (EtlJobTask etlJobTask : etlJobTaskList) {
            etlJobTask.setId(etlJobTaskMap.get(etlJobTask.getTaskName()));
            etlJobTask.setParamGroupCode(etlParamerGroupMap.get(etlJobTask.getGroupName()));
        }
        return SqlUtil.getSqlByList("ETL_JOB_TASK", etlJobTaskList, EtlJobTask.class);
    }


    /**
     * 生成 作业信息 的插入语句
     *
     * @param etlJobList
     * @return
     */
    private String getEtlJobSql(List<EtlJob> etlJobList) {
        if (StringUtils.isEmpty(etlJobList)) {
            return "";
        }
        StringBuffer buffer = new StringBuffer();
        for (EtlJob etlJob : etlJobList) {
            etlJobMap.put(etlJob.getJobName(), null);
        }
        // 查询标准库是否存在
        Map<String, String> etlJobMapStaMap = etlJobMapper.queryEtlJobAll(new QueryWrapper<>()).
                stream().collect(Collectors.toMap(e -> e.getJobName(), e -> e.getId()));
        etlJobMap.putAll(etlJobMapStaMap);
        for (Map.Entry<String, String> entry : etlJobMap.entrySet()) {
            if (StringUtils.isNotEmpty(entry.getValue())) {
                continue;
            }
            entry.setValue(IdUtils.fastSimpleUUID());
        }
        // 生成EtlJobTask的插入语句
        for (EtlJob etlJob : etlJobList) {
            String jobId = etlJobMap.get(etlJob.getJobName());
            String targetResourceId = etlResourceMap.get(etlJob.getTargetResourceId());
            etlJob.setId(jobId);
            etlJob.setLevelId(etlLevelMap.get(etlJob.getLevelName()));
            etlJob.setTargetResourceId(targetResourceId);
            etlJob.setTemplateId(etlTemplateMap.get(etlJob.getTemplateName()));
            // ETL_JOB_RESOURCE 的插入语句
            String[] arrSourceResource = etlJob.getSourceResourceId().split(",");
            for (String key : arrSourceResource) {
                String sourceResourceId = etlResourceMap.get(key);
                buffer.append("INSERT INTO ETL_JOB_RESOURCE (ID, JOB_ID, SOURCE_RESOURCE_ID, TARGET_RESOURCE_ID) ")
                        .append("VALUES('" + IdUtils.fastSimpleUUID() + "', '" + jobId + "', '" + sourceResourceId + "', '" + targetResourceId + "');").append("\r\n");
            }
        }
        buffer.append(SqlUtil.getSqlByList("ETL_JOB", etlJobList, EtlJob.class));
        return buffer.toString();
    }


    /**
     * 生成 作业明细 的插入语句
     *
     * @param etlJobDetailList
     * @return
     */
    private String getEtlJobDetailSql(List<EtlJobDetail> etlJobDetailList) {
        if (StringUtils.isEmpty(etlJobDetailList)) {
            return "";
        }
        // 生成EtlJobDetail的插入语句
        for (EtlJobDetail etlJobDetail : etlJobDetailList) {
            etlJobDetail.setId(IdUtils.fastSimpleUUID());
            etlJobDetail.setJobId(etlJobMap.get(etlJobDetail.getJobName()));
        }
        return SqlUtil.getSqlByList("ETL_JOB_DETAIL", etlJobDetailList, EtlJobDetail.class);
    }

    /**
     * 生成 作业明细 的插入语句
     *
     * @param etlJobTaskDetailList
     * @return
     */
    private String getEtlJobTaskDetailSql(List<EtlJobTaskDetail> etlJobTaskDetailList) {
        if (StringUtils.isEmpty(etlJobTaskDetailList)) {
            return "";
        }
        // 生成EtlJobTaskDetail的插入语句
        for (EtlJobTaskDetail etlJobTaskDetail : etlJobTaskDetailList) {
            etlJobTaskDetail.setTaskId(etlJobTaskMap.get(etlJobTaskDetail.getTaskName()));
            etlJobTaskDetail.setJobId(etlJobMap.get(etlJobTaskDetail.getJobName()));
            etlJobTaskDetail.setId(IdUtils.fastSimpleUUID());
        }
        return SqlUtil.getSqlByList("ETL_JOB_TASK_DETAIL", etlJobTaskDetailList, EtlJobTaskDetail.class);
    }
}
